//
//  MemberRegisterViewController.m
//  NewApp
//
//  Created by  on 12-6-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MemberRegisterViewController.h"
#import "MemberShipViewController.h"
#import "PathHelper.h"
#import "NAAccount.h"
#import "NAError.h"

@interface MemberRegisterViewController ()
/**
 *  Register request did finished
 */
- (void) registerRequestDidFinished:(RequestEngine *) engine withObject:(id) object;

/**
 *  Goto member home page
 */
- (void) goMemberHomePage;

@end

@implementation MemberRegisterViewController

@synthesize scrollView,textView,accountTextfield,passwordTextfield,confirmTextfield;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textView.autoresizingMask = UIViewAutoresizingNone;
    self.scrollView.contentSize =  CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    [self.engine registerInfos];
}

- (void) dealloc {
    [regReqEngine setDelegate:nil];
    if (regReqEngine != nil) {
        [regReqEngine release];
        regReqEngine = nil;
    }

    [super dealloc];
}

- (BOOL) canBecomeFirstResponder {
    return YES;
}

- (void) goMemberHomePage {
    MemberShipViewController *memHome = [[MemberShipViewController alloc] init];
    [self.navigationController pushViewController:memHome 
                                         animated:YES];
    [memHome release];
}

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *) object;
        [dict retain];
        
        NSString *title = [dict objectForKey:@"top_title"];
        if (title != nil && title.length > 0)
            self.titleLabel.text = title;
        
        NSString *clause = [dict objectForKey:@"clause"];
        if (title != nil && title.length > 0)
            self.textView.text = clause;
        
        [dict release];
        
    }
}

- (IBAction) registerButtonPressed:(id)sender {
    [self becomeFirstResponder];
    
    if(self.accountTextfield.text.length == 0) {
        [NAError showAlertMessageWithTitle:@"" message:@"請輸入帳戶名稱"];
        return;
    }
        
    if(self.passwordTextfield.text.length == 0) {
        [NAError showAlertMessageWithTitle:@"" message:@"請輸入密碼"];
        return;
    }
        
    if(self.confirmTextfield.text.length == 0) {
        [NAError showAlertMessageWithTitle:@"" message:@"請確認密碼"];
        return;
    }
    
    if (![self.confirmTextfield.text isEqualToString:self.passwordTextfield.text]) {
        [NAError showAlertMessageWithTitle:@"" 
                                   message:@"倆次輸入得密碼不一致"];
        return;
    }
    
    
    
    //Make sure one register request on one time
    if (regReqEngine != nil) 
        return;
    
    regReqEngine = [[RequestEngine alloc] initWithTarget:self action:@selector(registerRequestDidFinished:withObject:)];
    
    [regReqEngine registerMemberWithUserName:self.accountTextfield.text password:self.passwordTextfield.text];
}

- (void) registerRequestDidFinished:(RequestEngine *) engine withObject:(id) object {
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *) object;
        [dict retain];
        
        if ([[dict objectForKey:@"status"] isEqualToString:@"1"]) {
            NSArray *dataArray = [dict objectForKey:@"data"];
            if (dataArray != nil && dataArray.count > 0) {
                NAAccount *acc = [[NAAccount alloc] initWithDictionary:[dataArray objectAtIndex:0]];
                NSString *filePath = [[PathHelper documentDirectoryPathWithName:@"Acc"] 
                                      stringByAppendingPathComponent:@"accounts.info"];
                [NSKeyedArchiver archiveRootObject:acc toFile:filePath];
                
                [self goMemberHomePage];
            }
        }else {
            [NAError showAlertMessageWithTitle:@"" message:[dict objectForKey:@"msg"]];
        }
        
        [dict release];
    }
    else {
        [[NAError errorWithCode:NAError_REG_FAIL] showErrorMessage:nil];
    }

    NA_RELEASE_SAFELY(regReqEngine);
}

#pragma mark -
#pragma mark - UITextField delegates
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    
    BOOL canRetrun = (textField.text.length > 0) ? YES : NO;
    if (canRetrun) {
        [textField resignFirstResponder];
        self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        
        CGPoint offset = self.scrollView.contentOffset;
        offset.y = 0.f;
        
        [self.scrollView setContentOffset:offset animated:NO];
    }
        
    return canRetrun;
}

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height + 216.f);
    
    CGPoint offset = self.scrollView.contentOffset;
    offset.y = textField.frame.origin.y - 10.f;
    
    [self.scrollView setContentOffset:offset animated:YES];
}

- (void) textFieldDidEndEditing:(UITextField *)textField {
//    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height);
//    
//    CGPoint offset = self.scrollView.contentOffset;
//    offset.y = 0.f;
    
    //[self.scrollView setContentOffset:offset animated:NO];
}


@end
